package com.fileshare.web.handler;

import com.fileshare.web.dto.Result;
import com.fileshare.web.exception.TipException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;

/**
 * Api 统一异常处理
 */
@ControllerAdvice(annotations = RestController.class)
@ResponseBody
public class GlobalApiExceptionHandler {

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result<String> accessException(Exception e) {
        e.printStackTrace();
        return Result.failed(403, "AccessDenied").with();
    }

    @ExceptionHandler(value = TipException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> tipException(Exception e) {
        e.printStackTrace();
        return Result.failed(500, "InternalError").with();
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result<String> exception(Exception e) {
        e.printStackTrace();
        return Result.failed(404, "NotFound").with();
    }
}
